home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / tcsel003.zip / TPTIMER.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-16  |  6KB  |  164 lines

  1. {$S-,R-,I-,V-,B-}
  2.  
  3. {*********************************************************}
  4. {*                   TPTIMER.PAS 2.00                    *}
  5. {*                by TurboPower Software                 *}
  6. {*********************************************************}
  7.  
  8. unit TpTimer;
  9.   {-Allows events to be timed with 1 microsecond resolution}
  10.  
  11.  
  12.  
  13. interface
  14. const
  15.   TimerResolution = 1193181.667;
  16. procedure InitializeTimer;
  17.   {-Reprogram the timer chip to allow 1 microsecond resolution}
  18.  
  19. procedure RestoreTimer;
  20.   {-Restore the timer chip to its normal state}
  21.  
  22. function ReadTimer : LongInt;
  23.   {-Read the timer with 1 microsecond resolution}
  24.  
  25. function ElapsedTime(Start, Stop : LongInt) : Real;
  26.   {-Calculate time elapsed (in milliseconds) between Start and Stop}
  27.  
  28. function ElapsedTimeString(Start, Stop : LongInt) : string;
  29.   {-Return time elapsed (in milliseconds) between Start and Stop as a string}
  30.  
  31.   {==========================================================================}
  32.  
  33. implementation
  34.  
  35. var
  36.   SaveExitProc : Pointer;
  37.   Delta : LongInt;
  38.  
  39.   function Cardinal(L : LongInt) : Real;
  40.     {-Return the unsigned equivalent of L as a real}
  41.   begin                      {Cardinal}
  42.     if L < 0 then
  43.       Cardinal := 4294967296.0+L
  44.     else
  45.       Cardinal := L;
  46.   end;                       {Cardinal}
  47.  
  48.   function ElapsedTime(Start, Stop : LongInt) : Real;
  49.     {-Calculate time elapsed (in milliseconds) between Start and Stop}
  50.   begin                      {ElapsedTime}
  51.     ElapsedTime := 1000.0*Cardinal(Stop-(Start+Delta))/TimerResolution;
  52.   end;                       {ElapsedTime}
  53.  
  54.   function ElapsedTimeString(Start, Stop : LongInt) : string;
  55.     {-Return time elapsed (in milliseconds) between Start and Stop as a string}
  56.   var
  57.     R : Real;
  58.     S : string;
  59.   begin                      {ElapsedTimeString}
  60.     R := ElapsedTime(Start, Stop);
  61.     Str(R:0:3, S);
  62.     ElapsedTimeString := S;
  63.   end;                       {ElapsedTimeString}
  64.  
  65.   procedure InitializeTimer;
  66.     {-Reprogram the timer chip to allow 1 microsecond resolution}
  67.   begin                      {InitializeTimer}
  68.     {select timer mode 2, read/write channel 0}
  69.     Port[$43] := $34;        {00110100b}
  70.     inline($EB/$00);         {jmp short $+2 ;delay}
  71.     Port[$40] := $00;        {LSB = 0}
  72.     inline($EB/$00);         {jmp short $+2 ;delay}
  73.     Port[$40] := $00;        {MSB = 0}
  74.   end;                       {InitializeTimer}
  75.  
  76.   procedure RestoreTimer;
  77.     {-Restore the timer chip to its normal state}
  78.   begin                      {RestoreTimer}
  79.     {select timer mode 3, read/write channel 0}
  80.     Port[$43] := $36;        {00110110b}
  81.     inline($EB/$00);         {jmp short $+2 ;delay}
  82.     Port[$40] := $00;        {LSB = 0}
  83.     inline($EB/$00);         {jmp short $+2 ;delay}
  84.     Port[$40] := $00;        {MSB = 0}
  85.   end;                       {RestoreTimer}
  86.  
  87.   function ReadTimer : LongInt;
  88.     {-Read the timer with 1 microsecond resolution}
  89.   begin                      {ReadTimer}
  90.     inline(
  91.       $FA/                   {cli             ;Disable interrupts}
  92.       $BA/$20/$00/           {mov  dx,$20     ;Address PIC ocw3}
  93.       $B0/$0A/               {mov  al,$0A     ;Ask to read irr}
  94.       $EE/                   {out  dx,al}
  95.       $B0/$00/               {mov  al,$00     ;Latch timer 0}
  96.       $E6/$43/               {out  $43,al}
  97.       $EC/                   {in   al,dx      ;Read irr}
  98.       $89/$C7/               {mov  di,ax      ;Save it in DI}
  99.       $E4/$40/               {in   al,$40     ;Counter --> bx}
  100.       $88/$C3/               {mov  bl,al      ;LSB in BL}
  101.       $E4/$40/               {in   al,$40}
  102.       $88/$C7/               {mov  bh,al      ;MSB in BH}
  103.       $F7/$D3/               {not  bx         ;Need ascending counter}
  104.       $E4/$21/               {in   al,$21     ;Read PIC imr}
  105.       $89/$C6/               {mov  si,ax      ;Save it in SI}
  106.       $B0/$FF/               {mov  al,$0FF    ;Mask all interrupts}
  107.       $E6/$21/               {out  $21,al}
  108.       $B8/$40/$00/           {mov  ax,$40     ;read low word of time}
  109.       $8E/$C0/               {mov  es,ax      ;from BIOS data area}
  110.       $26/$8B/$16/$6C/$00/   {mov  dx,es:[$6C]}
  111.       $89/$F0/               {mov  ax,si      ;Restore imr from SI}
  112.       $E6/$21/               {out  $21,al}
  113.       $FB/                   {sti             ;Enable interrupts}
  114.       $89/$F8/               {mov  ax,di      ;Retrieve old irr}
  115.       $A8/$01/               {test al,$01     ;Counter hit 0?}
  116.       $74/$07/               {jz   done       ;Jump if not}
  117.       $81/$FB/$FF/$00/       {cmp  bx,$FF     ;Counter > $FF?}
  118.       $77/$01/               {ja   done       ;Done if so}
  119.       $42/                   {inc  dx         ;Else count int req.}
  120.       {done:}
  121.       $89/$5E/$FC/           {mov [bp-4],bx   ;set function result}
  122.       $89/$56/$FE);          {mov [bp-2],dx}
  123.   end;                       {ReadTimer}
  124.  
  125.   procedure Calibrate;
  126.     {-Calibrate the timer}
  127.   const
  128.     Reps = 1000;
  129.   var
  130.     I : Word;
  131.     L1, L2, Diff : LongInt;
  132.   begin                      {Calibrate}
  133.     Delta := MaxInt;
  134.     for I := 1 to Reps do begin
  135.       L1 := ReadTimer;
  136.       L2 := ReadTimer;
  137.       {use the minimum difference}
  138.       Diff := L2-L1;
  139.       if Diff < Delta then
  140.         Delta := Diff;
  141.     end;
  142.   end;                       {Calibrate}
  143.  
  144.   {$F+}
  145.   procedure OurExitProc;
  146.     {-Restore timer chip to its original state}
  147.   begin                      {OurExitProc}
  148.     ExitProc := SaveExitProc;
  149.     RestoreTimer;
  150.   end;                       {OurExitProc}
  151.   {$F-}
  152.  
  153. begin
  154.   {set up our exit handler}
  155.   SaveExitProc := ExitProc;
  156.   ExitProc := @OurExitProc;
  157.  
  158.   {reprogram the timer chip}
  159.   InitializeTimer;
  160.  
  161.   {adjust for speed of machine}
  162.   Calibrate;
  163. end.
  164.